package cn.ecnu.user.controller;


import cn.ecnu.common.Permission;
import cn.ecnu.user.interceptor.Own;
import cn.ecnu.user.service.UserService;
import cn.ecnu.user.vo.AdminAllUser;
import cn.ecnu.user.vo.AdminUserVo;
import cn.ecnu.user.vo.ChangeRoleVo;
import cn.ecnu.user.vo.UserInfoVo;
import cn.ecnu.utils.R;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @Own
    @GetMapping("/test")
    public R hello(HttpServletRequest request) {
        System.out.println(request.getHeader("userId"));
        System.out.println(request.getHeader("permission"));
        return R.ok().put("data", "hello");
    }

    @Own(permission = Permission.ADMIN)
    @GetMapping("/admin")
    public R admin() {
        return R.ok().put("data", "I'm admin");
    }


    // 管理员获取所有的用户
     // TODO: @Own(permission = Permission.ADMIN)
    // /user/list?pageNum=1
    @GetMapping("/list")
    public R getAllUsersPage(@RequestParam(value = "pageNum", required = false) Integer pageNum) {
        AdminAllUser vo = userService.getAllUsersPage(pageNum);
        return R.ok().put("data", vo);
    }

    @GetMapping("/condition")
    public R getFilterUser(@RequestParam(value = "uid", required = false) Integer uid,
                           @RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "phone", required = false) String phone) {
        List<AdminUserVo> users = userService.getAllUsers().getUsers();
        if (uid != null) {
            users = users.stream().filter((user) -> {
                return NumberUtils.compare(uid, user.getUserId()) == 0;
            }).collect(Collectors.toList());
        }

        if (name != null) {
            users = users.stream().filter((user) -> {
                return name.equals(user.getUserName());
            }).collect(Collectors.toList());
        }

        if (phone != null) {
            users = users.stream().filter((user) -> {
                return phone.equals(user.getPhone());
            }).collect(Collectors.toList());
        }
        return R.ok().put("data", users);
    }

    @GetMapping("/all")
    public R getAllUsers() {
        AdminAllUser vo = userService.getAllUsers();
        return R.ok().put("data", vo);
    }

    // 管理员修改用户权限
//    @Own(permission = Permission.ADMIN)
    @PostMapping("/changeRole")
    public R changeRole(@RequestBody ChangeRoleVo vo) {
        AdminUserVo adminUservo = userService.changeUserRole(vo);
        return R.ok().put("data", adminUservo);
    }

    // TODO: @Own(permission = Permission.ADMIN)
    @GetMapping("/delete/{uid}")
    public R deleteUserById(@PathVariable("uid") Integer uid) {
        userService.deleteUserById(uid);
        return R.ok();
    }


    @PostMapping("/update")
    public R updateUserById(@RequestBody UserInfoVo vo) {
        userService.updateUserById(vo);
        return R.ok();
    }



}
